//
//  FilterObject.m
//  图片故事
//
//  Created by 大帅逼 on 16/3/2.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "FilterObject.h"
#import "GPUImage.h"
#import "LocaData.h"
#import "FWApplyFilter.h"

@implementation FilterObject

+ (instancetype)shardFilterObject {

    static FilterObject *object = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        object = [[FilterObject alloc] init];
    });
    
    return object;
}

+ (NSArray *)readProFilterArray {

    NSArray *titleArray = [[NSArray alloc] initWithObjects:@"对比度",@"饱和度",@"曝光度",@"色温", nil];
    
    NSArray *imageArray = [[NSArray alloc] initWithObjects:@"contrast_icon",@"saturation_icon",@"exposure_icon",@"white_icon", nil];
    
    NSMutableArray *filterObjectArray = [[NSMutableArray alloc] init];
    
    for (int i = 0; i < titleArray.count; i++) {
        
        FilterModel *object = [[FilterModel alloc] init];
        
        object.filterImage = [imageArray objectAtIndex:i];
        
        object.filterTitle = [titleArray objectAtIndex:i];
        
        [filterObjectArray addObject:object];
    }
    
    return [filterObjectArray copy];
}

- (UIImage *)filterWithIndex:(NSInteger)index value:(CGFloat)value oldImage:(UIImage *)oldImage {
    
    UIImage *newImage;

    switch (index) {
        case 0:
            
            // 对比度情况下的处理图片(数值范围0 ～ 4 ，默认1)
            
            newImage = [FWApplyFilter changeValueForContrastFilter:value image:oldImage];
            
            break;
        case 1:
            
            // 饱和度情况的处理图片（数值范围0 ～ 2 ，默认1）
            
            newImage = [FWApplyFilter changeValueForSaturationFilter:value image:oldImage];
            
            break;
        case 2:
            
            // 曝光度情况的处理图片（数值范围－10 ～ 10 ，默认0）
            newImage = [FWApplyFilter changeValueForExposureFilter:value image:oldImage];
            
            break;
        case 3:
            
            // 色温情况的处理图片 (数值范围 1000 ～ 10000 ，默认5000)
            
            newImage = [FWApplyFilter changeValueForWhiteBalanceFilter:value image:oldImage];
            
            break;
            
        default:
            break;
    }
    
    return newImage;
}

#pragma mark -- 处理对比度情况下的图片
- (UIImage *) filterContrastWithValue : (CGFloat) value oldImage : (UIImage *) oldImage{

    GPUImageContrastFilter *filter = [[GPUImageContrastFilter alloc] init];
    
    // 设置处理度
    filter.contrast = value;
    
    // 设置处理的尺寸
    [filter forceProcessingAtSize:oldImage.size];
    
    // 将图片绑定
    GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:oldImage];
    
    [pic addTarget:filter];
    
    [pic processImage];
    
    [filter imageFromCurrentFramebuffer];
    
    [filter useNextFrameForImageCapture];
    
    UIImage *newImage = [filter imageFromCurrentFramebuffer];
    
    return newImage;
}

#pragma mark -- 处理饱和度情况的图片
- (UIImage *) filterSaturationWithValue : (CGFloat) value oldImage : (UIImage *) oldImage{
    
    GPUImageSaturationFilter *filter = [[GPUImageSaturationFilter alloc] init];
    
    // 设置处理度
    filter.saturation = value;
    
    // 设置处理的尺寸
    [filter forceProcessingAtSize:oldImage.size];
    
    // 将图片绑定
    GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:oldImage];
    
    [pic addTarget:filter];
    
    [pic processImage];
    
    [filter useNextFrameForImageCapture];
    
    UIImage *newImage = [filter imageFromCurrentFramebuffer];
    
    return newImage;
}

#pragma mark -- 处理曝光度情况的图片
- (UIImage *) filterExposureWithValue : (CGFloat) value oldImage : (UIImage *) oldImage{
    
    GPUImageExposureFilter *filter = [[GPUImageExposureFilter alloc] init];
    
    // 设置处理度
    filter.exposure = value;
    
    // 设置处理的尺寸
    [filter forceProcessingAtSize:oldImage.size];
    
    // 将图片绑定
    GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:oldImage];
    
    [pic addTarget:filter];
    
    [pic processImage];
    
    [filter useNextFrameForImageCapture];
    
    UIImage *newImage = [filter imageFromCurrentFramebuffer];
    
    return newImage;
}

#pragma mark -- 处理色温情况的图片
- (UIImage *) filterWhiteBalanceWithValue : (CGFloat) value oldImage : (UIImage *) oldImage{
    
    GPUImageWhiteBalanceFilter *filter = [[GPUImageWhiteBalanceFilter alloc] init];
    
    // 设置处理度
    filter.temperature = value;
    
    filter.tint = 0.0;
    
    // 设置处理的尺寸
    [filter forceProcessingAtSize:oldImage.size];
    
    // 将图片绑定
    GPUImagePicture *pic = [[GPUImagePicture alloc] initWithImage:oldImage];
    
    [pic addTarget:filter];
    
    [pic processImage];
    
    [filter useNextFrameForImageCapture];
    
    UIImage *newImage = [filter imageFromCurrentFramebuffer];
    
    return newImage;
}

#pragma mark -- 根据用户的设置来处理图片的质量
- (UIImage *)scaleImageWithImage:(UIImage *)image {

    UIImage *newImage;
    
    NSInteger photoQualit = [LocaData readPhotoQualit];
    
    switch (photoQualit) {
        case 0:
            
            newImage = [self scaleImageWithMaxValue:[@3072 integerValue] image:image];
            break;
        case 1:
            
            newImage = [self scaleImageWithMaxValue:[@2048 integerValue] image:image];
            break;
        case 2:
            
            newImage = [self scaleImageWithMaxValue:[@1024 integerValue] image:image];
            break;
            
        default:
            
            newImage =[self scaleImageWithMaxValue:[@1024 integerValue] image:image];
            break;
    }
    
    return newImage;
}

- (UIImage *) scaleImageWithMaxValue : (NSInteger) maxValue image : (UIImage *) oldImage{

    UIImage *newImage = [[UIImage alloc] init];
    
    NSData *oldImageData;
    
    if (UIImagePNGRepresentation(oldImage) != nil) {
        
        oldImageData = UIImagePNGRepresentation(oldImage);
    }else {
    
        oldImageData = UIImageJPEGRepresentation(oldImage, 1);
    }
    
    NSInteger imageValue = oldImageData.length / 1024.0;
    
    if (imageValue > maxValue) {

        CGFloat scale = (maxValue / 1.0) / imageValue;
        
        CGSize scaleSize = CGSizeMake(oldImage.size.width * scale, oldImage.size.height * scale);
        
        UIGraphicsBeginImageContext(scaleSize);
        
        [oldImage drawInRect:CGRectMake(0, 0, scaleSize.width, scaleSize.height)];
        
        newImage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        return newImage;
    }else {
    
        return oldImage;
    }
}

@end
